package chapter1.No1_8_暂停线程.b缺点_独占;

/**
 * *：使用suspend()如果使用不当容易照成公共对象独占的情况
 * Created by Administrator on 2016/11/9.
 */
public class SyncObject {
    public synchronized void doTest() {
        System.out.println("线程名："+Thread.currentThread().getName()
                +" doTest begin");
        if("a".equals(Thread.currentThread().getName())) {
            System.out.println("a线程");
            Thread.currentThread().suspend();
        }
        System.out.println("doTest end");
    }

    public static void main(String[] args) throws InterruptedException {
        final SyncObject syncObject = new SyncObject();
        Thread m1 = new Thread(){
            @Override
            public void run() {
                syncObject.doTest();
            }
        };
        m1.setName("a");
        m1.start();

        System.out.println("中场休息1秒钟");
        Thread.sleep(1000);

        Thread m2 = new Thread() {
            @Override
            public void run() {
                syncObject.doTest();
            }
        };
        m2.start();

        System.out.println("END");

    }
}
